<template>
  <div class="login-container">
    <div class="login-box">
      <h1 class="login-title">Simple Deploy</h1>
      <p class="login-subtitle">欢迎回来! 请登录您的账户</p>

      <el-form ref="loginFormRef" :model="loginForm" :rules="rules" class="login-form" @submit.prevent="handleLogin">
        <!-- 用户名输入框 -->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="用户名">
            <template #prefix>
              <el-icon>
                <user />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>

        <!-- 密码输入框 -->
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="密码" show-password>
            <template #prefix>
              <el-icon>
                <lock />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>

        <!-- 记住我和忘记密码 -->
        <div class="remember-forgot">
          <el-checkbox v-model="loginForm.remember">记住我</el-checkbox>
          <el-link type="primary" :underline="false">忘记密码?</el-link>
        </div>

        <!-- 登录按钮 -->
        <el-button type="primary" class="login-button" :loading="loading" @click="handleLogin">
          登录
        </el-button>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, nextTick } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { login } from '@/api/login'
import { useAuthStore } from '@/stores/user'

const router = useRouter()
const loading = ref(false)

// 登录表单数据
const loginForm = reactive({
  username: '',
  password: '',
  remember: false,
})

// 表单验证规则
const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
}

// 使用 ref 获取 el-form 实例
const loginFormRef = ref(null)
const userStore = useAuthStore()

// 登录处理函数
const handleLogin = async () => {
  loading.value = true
  try {
    // 验证表单
    const isValid = await loginFormRef.value.validate()
    if (!isValid) {
      // 如果验证失败，直接返回
      return
    }

    const { data } = await login({
      username: loginForm.username,
      password: loginForm.password,
      remember: loginForm.remember
    })

    // 登录成功
    userStore.login(data, loginForm.username)
    ElMessage.success('登录成功')

    // 确保数据更新后视图立即刷新
    await nextTick()

    router.push('/')
  } catch (error) {
    console.error('登录失败:', error)
    ElMessage.error('登录失败，请检查用户名和密码')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  min-height: 550px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #00b09b, #96c93d);
}

.login-box {
  background: white;
  padding: 40px;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
}

.login-title {
  text-align: center;
  font-size: 28px;
  font-weight: 600;
  color: #2c3e50;
  margin-bottom: 8px;
}

.login-subtitle {
  text-align: center;
  color: #7f8c8d;
  margin-bottom: 30px;
}

.login-form {
  margin-top: 20px;
}

.remember-forgot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 20px 0;
}

.login-button {
  width: 100%;
  padding: 12px;
  font-size: 16px;
  height: 35px;
}

:deep(.el-input__wrapper) {
  padding: 12px;
}

:deep(.el-input__inner) {
  font-size: 14px;
}
</style>
